package com.xxxx.server.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.Menu;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.Role;
import com.xxxx.server.service.IAdminRoleService;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.service.IMenuService;
import com.xxxx.server.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhoubin
 * @since 1.0.0
 */
@RestController
@RequestMapping("/system")
public class SystemController {

	@Autowired
	private IMenuService menuService;

	@Autowired
	private IAdminRoleService adminRoleService;

	@Autowired
	private IAdminService adminService;

	@Autowired
	private IRoleService roleService;

	@GetMapping("/menu")
	@ApiOperation(value = "查询所有菜单")
	public List<Menu> getAllMenusByAdminId(){
		return menuService.getAllMenusByAdminId();
	}


	@GetMapping("/admin")
	@ApiOperation(value = "初始化用户及用户角色")
	public List<Admin> initAllRoles(String keywords){

		return adminRoleService.initAllRoles(keywords);
	}

	@DeleteMapping("/admin/{id}")
	@ApiOperation(value = "逻辑删除角色")
	public RespBean  deleteAdmin(@PathVariable Integer id){

		Integer row = adminRoleService.updateAdmin(id);

		return RespBean.success("删除成功");
	}

	@PutMapping("/admin/")
	@ApiOperation(value = "启用禁用")
	public RespBean enabledChange(@RequestBody Admin admin){
		Boolean enabled = admin.getEnabled();
		Integer id = admin.getId();

		adminService.enabledChange(enabled,id);

		return RespBean.success("操作成功");
	}


	@GetMapping("/admin/roles")
	@ApiOperation(value = "初始化所有角色")
	public List<Role> initAllRoles(){

		List<Role> list = roleService.list();

		return list;
	}

	@PutMapping("/admin/role")
	@ApiOperation(value = "更新用户角色")
	public RespBean updateRole(Integer adminId,String rids){  // 3,   rids = 3,4,2

		adminRoleService.updateAdminRole(adminId, rids);

		return RespBean.success("更新成功！");
	}
}
